package com.monitor.system.service.impl;

import com.monitor.common.utils.DateUtils;
import com.monitor.common.utils.StringUtils;
import com.monitor.system.domain.DataBackupDetection;
import com.monitor.system.domain.ThresholdManagement;
import com.monitor.system.mapper.DataBackupDetectionMapper;
import com.monitor.system.service.IDataBackupDetectionService;
import com.monitor.system.service.IThresholdManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.monitor.common.constant.Constants.FOUR;
import static com.monitor.common.constant.Constants.ONE;
import static com.monitor.common.constant.Constants.THREE;
import static com.monitor.common.constant.Constants.TWO;
import static com.monitor.common.constant.Constants.ZERO;

/**
 * 数据备份检测Service业务层处理
 * 
 * @author monitor
 * @date 2024-06-06
 */
@Service
public class DataBackupDetectionServiceImpl implements IDataBackupDetectionService 
{
    @Autowired
    private DataBackupDetectionMapper dataBackupDetectionMapper;

    @Autowired
    private IThresholdManagementService service;
    /**
     * 查询数据备份检测
     * 
     * @param id 数据备份检测主键
     * @return 数据备份检测
     */
    @Override
    public DataBackupDetection selectDataBackupDetectionById(Long id)
    {
        return dataBackupDetectionMapper.selectDataBackupDetectionById(id);
    }
    /**
     * 查询数据备份检测列表
     *
     * @param dataBackupDetection 数据备份检测
     * @return 数据备份检测
     */
    @Override
    public List<DataBackupDetection> selectDataBackupDetectionList(DataBackupDetection dataBackupDetection) {
        List<DataBackupDetection> appList = dataBackupDetectionMapper.selectDataBackupDetectionList(dataBackupDetection);
        List<DataBackupDetection> newList = new ArrayList<>();

        ThresholdManagement thresholdManagement = new ThresholdManagement();
        thresholdManagement.setType("threshold_management_hard_device");
        ThresholdManagement threshold = service.selectThresholdManagement(thresholdManagement);
        if ( appList != null && appList.size() > 0 ) {
            for (DataBackupDetection obj : appList) {

                if(StringUtils.isNotNull(threshold)) {
                    // cpu利用率 状态hardware_situation
                    BigDecimal cpuUsageRate = new BigDecimal(obj.getCpuUsageRate());
                    // 内存利用率 状态hardware_situation
                    BigDecimal memoryUsageRate = new BigDecimal(obj.getMemoryUsageRate());
                    // 链路通信延时 network_status
                    BigDecimal linkCommunicationDelay = new BigDecimal(obj.getLinkCommunicationDelay());

                    if((memoryUsageRate.compareTo(new BigDecimal(threshold.getTimeConsuming1())) >= 0) ) {
                        obj.setHardwareSituation(FOUR);
                    }
                    // 状态：异常
                    else if((memoryUsageRate.compareTo(new BigDecimal(threshold.getTimeConsuming3())) >= 0) &&
                            (memoryUsageRate.compareTo(new BigDecimal(threshold.getTimeConsuming2())) <= 0)) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(THREE)) > 0) {
                            obj.setHardwareSituation(FOUR);
                        } else {
                            obj.setHardwareSituation(THREE);
                        }
                    }
                    // 状态：告警
                    else if((memoryUsageRate.compareTo(new BigDecimal(threshold.getTimeConsuming5())) >= 0) &&
                            (memoryUsageRate.compareTo(new BigDecimal(threshold.getTimeConsuming4())) <= 0)) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(TWO)) > 0) {
                            obj.setHardwareSituation(THREE);
                        } else {
                            obj.setHardwareSituation(TWO);
                        }
                    }
                    // 状态：延时
                    else if((memoryUsageRate.compareTo(new BigDecimal(threshold.getExtend1())) >= 0)
                            && (memoryUsageRate.compareTo(new BigDecimal(threshold.getTimeConsuming6())) <= 0) ) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(ONE)) > 0) {
                            obj.setHardwareSituation(TWO);
                        } else {
                            obj.setHardwareSituation("1");
                        }
                    }
                    // 状态：低于最小值状态设为正常
                    else if((memoryUsageRate.compareTo(new BigDecimal(threshold.getExtend1())) < 0)) {
                        obj.setHardwareSituation(ZERO);
                    }
                    // 内存
                    // 状态：故障
                    if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend2())) >= 0) ) {
                        obj.setHardwareSituation(FOUR);
                    }
                    // 状态：异常
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend4())) >= 0) &&
                            (cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend3())) <= 0)) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(THREE)) > 0) {
                            obj.setHardwareSituation(FOUR);
                        } else {
                            obj.setHardwareSituation(THREE);
                        }
                    }
                    // 状态：告警
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend6())) >= 0) &&
                            (cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend5())) <= 0)) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(TWO)) > 0) {
                            obj.setHardwareSituation(THREE);
                        } else {
                            obj.setHardwareSituation(TWO);
                        }
                    }
                    // 状态：延时
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend8())) >= 0) &&
                            (cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend7())) <= 0)) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(ONE)) > 0) {
                            obj.setHardwareSituation(TWO);
                        } else {
                            obj.setHardwareSituation(ONE);
                        }
                    }

                    // 状态：低于最小值 状态设为正常
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend8())) < 0)) {
                        if(new BigDecimal(obj.getHardwareSituation()).compareTo(new BigDecimal(ZERO)) < 0) {
                            obj.setHardwareSituation(ZERO);
                        }
                    }
                    // 網絡
                    // 状态：离线
                    if((linkCommunicationDelay.compareTo(new BigDecimal(threshold.getExtend9())) >= 0) ) {
                        obj.setNetworkStatus(TWO);
                    }
                    // 状态：延时
                    else if((linkCommunicationDelay.compareTo(new BigDecimal(threshold.getExtend11())) >= 0) &&
                            (linkCommunicationDelay.compareTo(new BigDecimal(threshold.getExtend10())) <= 0)) {
                        obj.setNetworkStatus(ONE);
                    }
                    // 状态：低于最小值 状态设为正常
                    else if((linkCommunicationDelay.compareTo(new BigDecimal(threshold.getExtend10())) < 0)) {
                        obj.setNetworkStatus(ZERO);
                        }
                    }
                int healthStatus = Math.max(Integer.valueOf(obj.getHardwareSituation()),Integer.valueOf(obj.getNetworkStatus()));
                // 运行状态
                obj.setHealthStatus(String.valueOf(healthStatus));

                dataBackupDetectionMapper.updateDataBackupDetection(obj);
                newList = dataBackupDetectionMapper.selectDataBackupDetectionList(dataBackupDetection);
                }
            }
        return newList;
    }
    /**
     * 新增数据备份检测
     * 
     * @param dataBackupDetection 数据备份检测
     * @return 结果
     */
    @Override
    public int insertDataBackupDetection(DataBackupDetection dataBackupDetection)
    {
        dataBackupDetection.setCreateTime(DateUtils.getNowDate());
        return dataBackupDetectionMapper.insertDataBackupDetection(dataBackupDetection);
    }

    /**
     * 修改数据备份检测
     * 
     * @param dataBackupDetection 数据备份检测
     * @return 结果
     */
    @Override
    public int updateDataBackupDetection(DataBackupDetection dataBackupDetection)
    {
        dataBackupDetection.setUpdateTime(DateUtils.getNowDate());
        return dataBackupDetectionMapper.updateDataBackupDetection(dataBackupDetection);
    }

    /**
     * 批量删除数据备份检测
     * 
     * @param ids 需要删除的数据备份检测主键
     * @return 结果
     */
    @Override
    public int deleteDataBackupDetectionByIds(Long[] ids)
    {
        return dataBackupDetectionMapper.deleteDataBackupDetectionByIds(ids);
    }

    /**
     * 删除数据备份检测信息
     * 
     * @param id 数据备份检测主键
     * @return 结果
     */
    @Override
    public int deleteDataBackupDetectionById(Long id)
    {
        return dataBackupDetectionMapper.deleteDataBackupDetectionById(id);
    }
}
